package com.xuanzheng.company.servlet;

import com.google.gson.Gson;
import com.xuanzheng.company.entity.CompanyEmployee;
import com.xuanzheng.company.entity.User;
import com.xuanzheng.company.service.CompanyEmployeeService;
import com.xuanzheng.company.service.impl.CompanyEmployeeServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/getResignations")
public class GetResignationsServlet extends HttpServlet {
    private CompanyEmployeeService companyEmployeeService = new CompanyEmployeeServiceImpl();
    private Gson gson = new Gson();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("application/json;charset=UTF-8");
        PrintWriter out = response.getWriter();
        Map<String, Object> result = new HashMap<>();
        
        // 获取当前登录用户
        HttpSession session = request.getSession(false);
        if (session == null || session.getAttribute("user") == null) {
            result.put("success", false);
            result.put("message", "请先登录");
            out.print(gson.toJson(result));
            return;
        }
        
        User currentUser = (User) session.getAttribute("user");
        
        // 检查是否是公司负责人
        if (currentUser.getRole() != 1) {
            result.put("success", false);
            result.put("message", "只有公司负责人可以查看离职申请");
            out.print(gson.toJson(result));
            return;
        }
        
        String companyId = currentUser.getCompanyId();
        if (companyId == null || companyId.isEmpty()) {
            result.put("success", false);
            result.put("message", "您尚未加入公司");
            out.print(gson.toJson(result));
            return;
        }
        
        // 获取公司所有离职申请
        List<CompanyEmployee> resignations = companyEmployeeService.getResignationsByCompanyId(companyId);
        
        result.put("success", true);
        result.put("data", resignations);
        
        out.print(gson.toJson(result));
    }
} 